<?php
include('../lib/phpQuery-onefile.php');

$data_json=<<<json_str
{"Types":[{"Id":0,"Name":"全部"},{"Id":2,"Name":"動畫"},{"Id":31,"Name":"季度全集"},{"Id":3,"Name":"漫畫"},{"Id":41,"Name":"港台原版"},{"Id":42,"Name":"日文原版"},{"Id":4,"Name":"音樂"},{"Id":43,"Name":"動漫音樂"},{"Id":44,"Name":"同人音樂"},{"Id":15,"Name":"流行音樂"},{"Id":6,"Name":"日劇"},{"Id":7,"Name":"ＲＡＷ"},{"Id":9,"Name":"遊戲"},{"Id":17,"Name":"電腦遊戲"},{"Id":18,"Name":"電視遊戲"},{"Id":19,"Name":"掌機遊戲"},{"Id":20,"Name":"網絡遊戲"},{"Id":21,"Name":"遊戲周邊"},{"Id":12,"Name":"特攝"},{"Id":1,"Name":"其他"}]}
json_str;

header("Access-Control-Allow-Origin:*");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
echo $data_json;
exit;

$params=array(
    "host_uri"=>"https://share.dmhy.org",
);
$params["type_and_subgroup_uri"]="{$params['host_uri']}/topics/advanced-search?team_id=0&sort_id=0&orderby=";
// var_dump($params);


$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $params["type_and_subgroup_uri"],
    CURLOPT_RETURNTRANSFER => true,
    //     CURLOPT_ENCODING => "utf-8",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_SSL_VERIFYPEER=>false,
    CURLOPT_SSL_VERIFYHOST=>false,
    //CURLOPT_POSTFIELDS => json_encode($data_array),
    CURLOPT_HTTPHEADER => array(
        "Content-type:text/html;charset='utf-8'",
        "Accept:text/html",
        "Cache-Control:no-cache",
        "Pragma:no-cache",
    ),
));


$response_text = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    return $data_json;
}
// echo $response_text;

$data_array=array("Types"=>array());
$doc = phpQuery::newDocumentHTML($response_text);
phpQuery::selectDocument($doc);

$sort_options=pq("select#AdvSearchSort option");
// echo "trs size:".count($trs)."<br>";

foreach ($sort_options as $sort_option) {
    $resource_data=parse_list_group($sort_option);
    array_push($data_array["Types"],$resource_data);
}

$json_result=json_encode($data_array,JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
header("Access-Control-Allow-Origin:*");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
echo $json_result;

/*
 <p>在下列分類中搜索：<select name="sort_id" id="AdvSearchSort"><option value="0">全部</option><option value="2" style="color: red">動畫</option><option value="31" style="color: red">季度全集</option><option value="3" style="color: green">漫畫</option><option value="41" style="color: green">港台原版</option><option value="42" style="color: green">日文原版</option><option value="4" style="color: purple">音樂</option><option value="43" style="color: purple">動漫音樂</option><option value="44" style="color: purple">同人音樂</option><option value="15" style="color: purple">流行音樂</option><option value="6" style="color: blue">日劇</option><option value="7" style="color: orange">ＲＡＷ</option><option value="9" style="color: #0eb9e7">遊戲</option><option value="17" style="color: #0eb9e7">電腦遊戲</option><option value="18" style="color: #0eb9e7">電視遊戲</option><option value="19" style="color: #0eb9e7">掌機遊戲</option><option value="20" style="color: #0eb9e7">網絡遊戲</option><option value="21" style="color: #0eb9e7">遊戲周邊</option><option value="12" style="color: brown">特攝</option><option value="1" style="color: black">其他</option></select></p>
 <p>在下列聯盟中搜索：<select name="team_id" id="AdvSearchTeam"><option value="0">全部</option><option value="117">動漫花園</option><option value="669">喵萌奶茶屋</option><option value="185">极影字幕社</option><option value="619">桜都字幕组</option><option value="604">c.c动漫</option><option value="755">GMTeam</option><option value="648">魔星字幕团</option><option value="731">星空字幕组</option><option value="563">花園壓制組</option><option value="657">LoliHouse</option><option value="151">悠哈C9字幕社</option><option value="241">幻樱字幕组</option><option value="47">爱恋字幕社</option><option value="390">天使动漫论坛</option><option value="303">动漫国字幕组</option><option value="37">雪飄工作室(FLsnow)</option><option value="283">千夏字幕组</option><option value="574">梦蓝字幕组</option><option value="710">咪梦动漫组</option><option value="504">LoveEcho!</option><option value="592">未央阁联盟</option><option value="288">诸神kamigami字幕组</option><option value="434">风之圣殿</option><option value="650">SweetSub</option><option value="88">动音漫影</option><option value="581">VCB-Studio</option><option value="321">轻之国度</option><option value="765">爱咕字幕组</option><option value="407">DHR動研字幕組</option><option value="675">AikatsuFans</option><option value="666">中肯字幕組</option><option value="576">银色子弹字幕组</option><option value="459">紫音動漫&amp;發佈組</option><option value="769">动漫萌</option><option value="228">KRL字幕组</option><option value="703">届恋字幕组</option><option value="649">云光字幕组</option><option value="779">银刃字幕组</option><option value="454">风车字幕组</option><option value="772">IET字幕組</option><option value="630">枫叶字幕组</option><option value="526">东京不够热</option><option value="225">鈴風字幕組</option><option value="673">VRAINSTORM</option><option value="680">Little字幕组</option><option value="626">驯兽师联盟</option><option value="520">豌豆字幕组</option><option value="786">棒聯貼吧字幕組</option><option value="706">K&amp;W-RAWS</option><option value="485">天空树双语字幕组</option><option value="785">野比家字幕组</option><option value="561">钉铛字幕组</option><option value="767">天月動漫&amp;發佈組</option><option value="447">夢幻戀櫻</option><option value="468">喵萌茶会字幕组</option><option value="781">SW字幕组</option><option value="784">Voice Memories</option><option value="734">TD-RAWS</option><option value="753">柠檬水字幕组</option><option value="764">MCE汉化组</option><option value="49">华盟字幕社</option><option value="701">狐狸小宮</option><option value="110">天香字幕社</option><option value="641">冷番补完字幕组</option><option value="727">2B4B</option><option value="222">A.I.R.nesSub</option><option value="788">ebbSub</option><option value="532">傲娇零字幕组</option><option value="652">SFEO-Raws</option><option value="790">WBX-SUB</option><option value="757">RvE发布组</option><option value="759">红鸟窝字幕组</option><option value="763">光之家族字幕组</option><option value="75">柯南事务所</option><option value="699">小花花同盟戰線</option><option value="613">AI-Raws</option><option value="770">檸檬好酸字幕組</option><option value="796">臭臭动漫整合</option></select></p>
 <p>按下列順序排列：<select name="order"><option value="date-desc">發佈時間從後往前</option><option value="date-asc">發佈時間從前往後</option><option value="rel">相關度</option></select></p>
 <p><a href="/announce#ann108">瞭解更多</a></p>
 */
function parse_list_group($group_option){
    $field_name_array=array("Id","Name");
    $field_data_array=array();
    
    $field_data_array["Id"]=intval(pq($group_option)->attr("value"));
    $field_data_array["Name"]=pq($group_option)->text();
    
    foreach($field_name_array as $field_name){
        $field_data_array[$field_name]=trim($field_data_array[$field_name]);
    }
    
    return $field_data_array;
}
?>